package main

import (
	"fmt"
	"math/rand"
	"sort"
	"time"
)

func work1() {
	s1 := make([]int, 3, 4)
	s2 := append(s1, 1)
	// s1内元素
	fmt.Printf("s1 %v %p %p %d %d\n", s1, &s1, &s1[0], len(s1), cap(s1))
	// s2内元素
	fmt.Printf("s2 %v %p %p %d %d\n", s2, &s2, &s2[0], len(s2), cap(s2))
	// s1、s2共用底层数组，s1修改元素会影响s2
	s1[0] = 100
	fmt.Printf("s1 %v %p %p %d %d\n", s1, &s1, &s1[0], len(s1), cap(s1))
	fmt.Printf("s2 %v %p %p %d %d\n", s2, &s2, &s2[0], len(s2), cap(s2))
	// 如果s2再增加一个元素会扩容，s1、s2不在共用底层数组
	s2 = append(s2, 2)
	fmt.Printf("s1 %v %p %p %d %d\n", s1, &s1, &s1[0], len(s1), cap(s1))
	fmt.Printf("s2 %v %p %p %d %d\n", s2, &s2, &s2[0], len(s2), cap(s2))
}

func work2() {
	s1 := [...]int{1, 4, 9, 16, 2, 5, 10, 15}
	s2 := make([]int, len(s1)-1)
	for i := 0; i < len(s1)-1; i++ {
		s2[i] = s1[i] + s1[i+1]
	}
	fmt.Println(s1)
	fmt.Printf("数组相邻数的和为：\n%v\n", s2)
}

func work3() {
	num := 100
	nums := make([]int, 0, num)
	r := rand.New(rand.NewSource(time.Hour.Nanoseconds()))
	count := make(map[int]int)
	keys := make([]int, 0, num)
	for i := 0; i < num; i++ {
		// 100个整数 [-100, 100]
		n := r.Intn(201) - 100
		nums = append(nums, n)
		if _, ok := count[n]; !ok {
			count[n] = 0
			keys = append(keys, n)
		}
		count[n] += 1
	}
	fmt.Println(nums)
	fmt.Println(count, len(count))
	// 去重 降序排列 记录重复次数
	sort.Sort(sort.Reverse(sort.IntSlice(keys)))
	for _, k := range keys {
		fmt.Println(k, count[k])
	}
}

func main() {
	fmt.Println("第一题**************************")
	work1()
	fmt.Println("第二题**************************")
	work2()
	fmt.Println("第三题**************************")
	work3()
}
